package com.smedi.ismedi.cronjob.core.infrastructure.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.cronjob.core.domain.aggregatemodel.JobRun;
import com.smedi.ismedi.cronjob.core.domain.repository.JobRunRepository;
import com.smedi.ismedi.cronjob.core.infrastructure.persistence.model.JobRunEntity;
import com.smedi.ismedi.cronjob.core.infrastructure.persistence.mybatis.mapper.JobRunMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author u
 */
@Slf4j
@Repository
@AllArgsConstructor
public class CronjobRunRepositoryImpl extends ServiceImpl<JobRunMapper, JobRunEntity> implements JobRunRepository, IService<JobRunEntity> {
    private final DozerUtil dozerUtil;

    @Override
    public Boolean doDelete(Long id) {
        return removeById(id);
    }

    @Override
    public Boolean doDelete(List<Long> ids) {
        return removeByIds(ids);
    }

    @Override
    public JobRun doInsert(JobRun jobRun) {
        JobRunEntity jobRunEntity = dozerUtil.convertor(jobRun, JobRunEntity.class);
        save(jobRunEntity);
        return dozerUtil.convertor(jobRunEntity, JobRun.class);
    }

    @Override
    public List<JobRun> doInsert(List<JobRun> jobRuns) {
        List<JobRunEntity> jobRunEntities = dozerUtil.convertor(jobRuns, JobRunEntity.class);
        saveBatch(jobRunEntities);
        return dozerUtil.convertor(jobRunEntities, JobRun.class);
    }

    @Override
    public JobRun doUpdate(JobRun jobRun) {
        JobRunEntity jobRunEntity = dozerUtil.convertor(jobRun, JobRunEntity.class);
        updateById(jobRunEntity);
        return dozerUtil.convertor(jobRunEntity, JobRun.class);
    }

    @Override
    public List<JobRun> doUpdate(List<JobRun> jobRuns) {
        List<JobRunEntity> jobRunEntities = dozerUtil.convertor(jobRuns, JobRunEntity.class);
        updateBatchById(jobRunEntities);
        return dozerUtil.convertor(jobRunEntities, JobRun.class);
    }

    @Override
    public JobRun doGet(Long id) {
        return dozerUtil.convertor(getById(id), JobRun.class);
    }

    @Override
    public List<JobRun> doList(List<Long> ids) {
        return dozerUtil.convertor(listByIds(ids), JobRun.class);
    }

    @Override
    public List<JobRun> doList(JobRun jobRun) {
        JobRunEntity jobRunEntity = dozerUtil.convertor(jobRun, JobRunEntity.class);
        QueryWrapper<JobRunEntity> queryWrapper = new QueryWrapper<>(jobRunEntity);
        return dozerUtil.convertor(list(queryWrapper), JobRun.class);
    }
}
